﻿Imports System.Linq
Imports System.Data
Imports System.Web
Imports Equipment.DataAccess

Partial Public Class Requirement

#Region "Properties"
    Private _db As EquipmentDataContext
    Public Property DBContext As EquipmentDataContext
        Get
            Return _db
        End Get
        Set(ByVal value As EquipmentDataContext)
            _db = value
        End Set
    End Property

    Private _measureList As List(Of RequirementMeasure)
    Public Property Measures As List(Of RequirementMeasure)
        Get
            If _measureList Is Nothing Then

                _measureList = New List(Of RequirementMeasure)

                For Each measure In (From m In Database.Instance.RequirementMeasures Where m.RequirementID = Me.ID)
                    _measureList.Add(measure)
                Next

            End If
            Return _measureList
        End Get
        Set(ByVal value As List(Of RequirementMeasure))
            _measureList = value
        End Set
    End Property

#End Region

#Region "Public Section"
    Public Shared Function RetrieveAll() As IEnumerable(Of Requirement)
        Dim db As EquipmentDataContext = Database.Instance
        Return db.Requirements.OrderBy(Function(r) r.Name)
    End Function

    Public Shared Function RetrieveAll(ByVal assetTypeID As Integer) As IEnumerable(Of Requirement)
        Dim db As EquipmentDataContext = Database.Instance
        If assetTypeID > 0 Then
            Return From br In Database.Instance.Requirements Where br.AssetTypeID = assetTypeID
        Else
            Return RetrieveAll()
        End If
    End Function

    Public Shared Function RetrieveAllByAssetType(ByVal assetTypeID As Integer) As IEnumerable(Of Requirement)
        Return From br In Database.Instance.Requirements Where br.AssetTypeID = assetTypeID
    End Function


    Public Shared Function RetrieveAll(ByVal assetTypeID As Integer, ByVal frequencyList As List(Of RequirementFrequency)) As IEnumerable(Of Requirement)
        Return RetrieveAll().Where(Function(br) frequencyList.Contains(br.FrequencyType))
    End Function

    Public Shared Function GetByPrimaryKey(ByVal id As Integer) As Requirement
        Dim db As EquipmentDataContext = Database.Instance
        Dim result = From r In db.Requirements Where r.ID = id
        Dim reqEntity As Requirement = result.FirstOrDefault()
        reqEntity.DBContext = db
        Return reqEntity
    End Function

    Public Shared Function Insert(ByVal entity As Requirement) As Requirement
        Dim db As EquipmentDataContext = Database.Instance
        db.Requirements.InsertOnSubmit(entity)
        Try
            db.SubmitChanges()

            For Each measure In entity.Measures
                measure.RequirementID = entity.ID
                db.RequirementMeasures.InsertOnSubmit(measure)
            Next
            db.SubmitChanges()
        Catch ex As Exception
            ' logger.Error(exception.Message + ":" + exception.StackTrace);
        End Try

        Return entity
    End Function

    Public Sub Save()
        If Me.DBContext IsNot Nothing Then
            Dim _dataBase As EquipmentDataContext = Me.DBContext
            Dim measures = From rm In _dataBase.RequirementMeasures Where rm.RequirementID = Me.ID Select rm

            Try

                ' Measures
                For Each measure In measures
                    Dim currMeasure As RequirementMeasure = Me.Measures.SingleOrDefault(Function(m) m.ID = measure.ID)
                    If currMeasure Is Nothing Then
                        _dataBase.RequirementMeasures.DeleteOnSubmit(measure)
                    End If
                Next
                For Each measure In Me.Measures
                    Dim currMeasure As RequirementMeasure = measures.SingleOrDefault(Function(m) m.ID = measure.ID)
                    If measure.ID = 0 Or currMeasure Is Nothing Then
                        measure.RequirementID = Me.ID
                        _dataBase.RequirementMeasures.InsertOnSubmit(measure)
                    End If
                Next

                _dataBase.SubmitChanges()
            Catch ex As Exception
                ' logger.Error(exception.Message + ":" + exception.StackTrace);
            End Try
        End If

    End Sub

    Public Shared Function GetFrequencyDescriptionByType(ByVal type As RequirementFrequency) As String
        Select Case type
            Case RequirementFrequency.EveryXDays, _
                RequirementFrequency.EveryXHours, _
                RequirementFrequency.TwiceDaily
                Return "Daily"
            Case RequirementFrequency.EveryXMonth
                Return "Monthly"
            Case RequirementFrequency.EveryXWeeks
                Return "Weekly"
            Case RequirementFrequency.EveryXYears
                Return "Annually"
            Case RequirementFrequency.WhenUsed
                Return "When Used"
        End Select
        Return String.Empty
    End Function
#End Region

End Class
